import requests
from bs4 import  BeautifulSoup
class qqmusic:
    def getSongList(self):
        res_music = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=68859140523195182&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w=%E5%91%A8%E6%9D%B0%E4%BC%A6&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0')
        # 请求html，得到response

        json_music = res_music.json()

        song_list = json_music['data']['song']['list']

        for music in song_list:

            print('歌曲名称：'+music['name'])
            print('所属专辑：'+music['album']['name'])
            print('播放时长：'+str(+music['interval'])+'秒')
            print('播放地址：https://y.qq.com/n/yqq/song/'+music['mid']+'.html\n\n')
            print('===================================')



    def getComment(self):

        url = 'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'

        commentid = ''

        headers = {

            'origin':'https://y.qq.com',

            'referer': 'https://y.qq.com/n/yqq/song/004Z8Ihr0JIu5s.html',

            'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'


        }

        for x in range(20):
            params = {
                'g_tk': '5381',
                'loginUin': '0',
                'hostUin': '0',
                'format': 'json',
                'inCharset': 'utf8',
                'outCharset': 'GB2312',
                'notice': '0',
                'platform': 'yqq.json',
                'needNewCode': '0',
                'cid': '205360772',
                'reqtype': '2',
                'biztype': '1',
                'topid': '102065756',
                'cmd': '8',
                'needcommentcrit': '0',
                'pagenum': str(x),
                'pagesize': '25',
                'lasthotcommentid': commentid,
                'domain': 'qq.com',
                'ct': '24',
                'cv': '101010  '
            }

            res = requests.get(url,params=params,headers=headers)

            resjson = res.json()

            commentList = resjson['comment']['commentlist']

            musicName = resjson['topic_name']

            print('')

            for comment in commentList:

                commentContent = comment['rootcommentcontent']

                print('歌曲名：'+musicName+'\t 歌曲评论：'+commentContent)

            commentid = commentList[24]['commentid']



    def getLyrics(self):

        url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'

        params = {

            'ct':'24',
            'qqmusic_ver':'1298',
            'new_json':'1',
            'remoteplace':'txt.yqq.song',
            'searchid':'61190558053343224',
            't':'0',
            'aqqr':'1',
            'cr':'1',
            'catZhida':'1',
            'lossless':'0',
            'flag_qc':'0',
            'p':'1',
            'n':'10',
            'w':'%E5%91%A8%E6%9D%B0%E4%BC%A6',
            'g_tk':'5381',
            'loginUin':"0",
            'format':'json',
            'inCharset':'utf-8',
            'outCharset':'utf-8',
            'notice':'0',
            'platform':'yqq.json',
            'needNewCode':'0'
        }

        headers = {

            'origin': 'https://y.qq.com',

            'referer': 'https://y.qq.com/portal/search.html',

            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'

        }

        res = requests.get(url,params=params,headers=headers)

        resjson = res.json()

        song_list = resjson['data']['song']['list']


        for song in song_list:
            song_name = song['name']

            print(song_name)

            song_mid = song['mid']

            musiccid = song['id']

            song_url = 'https://y.qq.com/n/yqq/song/'+song_mid+'.html'


            # lyric_headers = {
            #
            #     'origin': 'https://y.qq.com',
            #
            #     'referer': 'https://y.qq.com/n/yqq/song/'+song_mid+'.html',
            #
            #     'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
            #
            # }


            lyric_res = requests.get(song_url)

            html = lyric_res.text

            bs = BeautifulSoup(html,'html.parser')

            lyrics = bs.find_all('div',id='lrc_content')

            for lyric in lyrics:

                ly = lyric.find('p')

                print(ly)

a = qqmusic()

a.getLyrics()